package com.hs.eric.controller;

import com.alibaba.fastjson.JSON;
import com.hs.eric.entity.TreeItem;
import com.hs.eric.entity.User;
import com.hs.eric.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * Description:
 * User: eric
 * Date: 2015/5/3
 * Time: 17:05
 */
@Controller
@RequestMapping(value = "user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "list")
    public String list(Model model) {
        List<User> list = userService.getUsers();
        model.addAttribute("list", list);
        List<TreeItem> items = userService.selectAgencyBeTree();
        for (TreeItem treeItem : items) {
            if (treeItem.getPid()==null)
            {
                treeItem.setPid(0);
            }
        }
        String treeData = JSON.toJSONString(items);
        model.addAttribute("treeData",treeData.replaceAll("\"pid\"","pId"));

        return "users";
    }
}